package com.obor.quartz.job.service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.obor.quartz.job.JobInfo;
import com.obor.quartz.job.JobManager;
import com.obor.quartz.job.mapper.JobInfoMaper;

/**
* 
* @Company OBOR
* @Author 周楠
* @Description
* 2019年4月9日
*
*/
@Service
public class JobService  {
	
	@Autowired
	private MongoService mongoService;
	
	@Autowired 
	private JobManager jobManager;
	

	
	public void add(JobInfo job) {
		// TODO Auto-generated method stub
		
		
		JobInfo  ji= mongoService.findJobStatus(job);
		
		if(Objects.isNull(ji)) {
		job.setStatus("create");
		job.setCreateTime(new Date());
		mongoService.insertStatus(job);
		}else {
			
			job.setStatus("recreate");
			job.setUpdateTime(new Date());
			mongoService.jobStatsUpdate(job);

		}
		
		if(job.getType().equals(JobInfo.TYPE_MS)) {
			
			job.setBeanClass("com.obor.quartz.job.biz.MsApiJob");
			
		}
		else if(job.getType().equals(JobInfo.TYPE_API)) {
			job.setBeanClass("com.obor.quartz.job.biz.OutterApiJob");
		}
		
		jobManager.addJob(job);
		
	}
	
	
	public void update(JobInfo job) {
		
		
		try {
			jobManager.delJob(job.getKey(), job.getGroup());
		
			jobManager.addJob(job);
			
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	 } 
	

	
	public void del(String jobkey, String group) throws SchedulerException {
		// TODO Auto-generated method stub
		
		jobManager.delJob(jobkey, group);
		
	}

	public void stop(String jobkey, String group) throws SchedulerException {
		// TODO Auto-generated method stub
		
		
		jobManager.delJob(jobkey, group);
		
	}

	public void pause(String jobkey, String group) throws SchedulerException {
		// TODO Auto-generated method stub
		
		
		jobManager.pauseJob(jobkey, group);
	}


	public void resume(String jobkey, String group) throws SchedulerException {
		// TODO Auto-generated method stub
		
		jobManager.resumeJob(jobkey, group);
		
	}

	public List<Map<String, Object>> list() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	public void serverStart() throws SchedulerException {
	
		jobManager.schedulerStart();
		
	}
	
	

}
